<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="logo-img">
        <img src="../assets/logo1.png"
             alt="">
        <span>坚果商城后台管理系统</span>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px' ">
        <div class="toggle-btn"
             @click="toggleCollapse">{{isCollapseIcon}}</div>
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#ffd04b"
                 :unique-opened="true"
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="activePath">
          <el-submenu v-for="item in menulist"
                      :key="item.id"
                      :index="item.id + ''">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>

      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      isCollapse: false,
      isCollapseIcon: '|||',
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
      if (this.isCollapse === true) {
        this.isCollapseIcon = '＞'
      } else {
        this.isCollapseIcon = '|||'
      }
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.logo-img {
  height: 100%;
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
  font-weight: 500;
  letter-spacing: 0.1em;
}
.logo-img img {
  height: 90%;
  width: auto;
  margin-right: 10px;
}
.el-menu {
  border: 0px;
}
.toggle-btn {
  background-color: #4b5063;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
